<?php

class User extends BaseActiveRecord
{
    public $login;
    public $password;
    public $remember = false;

    public function tableName()
    {
        return '{{users}}';
    }

    public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

    public function rules()
    {
        return array
        (
            array('login, password', 'required'),
            array('remember', 'boolean'),
            array('password', 'authenticate', 'on' => 'login')
        );
    }

    public function authenticate($attribute, $params)
    {
        $oIdentity = new UserIdentity($this->login, $this->password);
        
        if( !$oIdentity->authenticate() )
        {
            $this->addError('password','Incorrect login or password.');
        }
        else
        {
            if ( $this->remember )
            {
                Yii::app()->user->login($oIdentity, 3600*24*7);
            }
            else
            {
                Yii::app()->user->login($oIdentity);
            }
        }
    }

    public function  checkboxes()
    {
        return array
        (
            'remember'
        );
    }
}